const path = require('path');
const mkdirp = require('mkdirp');
const baseSaveDir = path.join(think.ROOT_PATH, 'www', 'static', 'upload', 'imgs');
const fs = require('fs');
fs.stat(baseSaveDir, (err, stats) => {
  if ((err && err.code === 'ENOENT') || !stats.isDirectory()) {
    mkdirp(baseSaveDir);
  }
});

module.exports = class extends think.Service {
  saveImageFile(imgFile) {
    think.logger.info(imgFile);
    const filename = think.uuid(32) + '.jpg';
    const is = fs.createReadStream(imgFile.path);
    const os = fs.createWriteStream(path.join(baseSaveDir, filename));
    is.pipe(os);
    return '/static/upload/imgs/' + filename;
  }
};
